<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Lexer examples</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="parle.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="parle.examples.parser.html">Parser examples »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="parle.examples.html">范例</a></li>
    <li>Lexer examples</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="parle.examples.lexer" class="section">
  <h2 class="title">Lexer examples</h2>
  <div class="example" id="example-4715">
   <p><strong>示例 #1 Tokenize comma separated integer list</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">Parle</span><span style="color: #007700">\</span><span style="color: #0000BB">Token</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">Parle</span><span style="color: #007700">\</span><span style="color: #0000BB">Lexer</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">Parle</span><span style="color: #007700">\</span><span style="color: #0000BB">LexerException</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;name&nbsp;=&gt;&nbsp;id&nbsp;*/<br /></span><span style="color: #0000BB">$token&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"COMMA"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"CRLF"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"DECIMAL"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,<br />);<br /></span><span style="color: #FF8000">/*&nbsp;id&nbsp;=&gt;&nbsp;name&nbsp;*/<br /></span><span style="color: #0000BB">$tokenIdToName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_flip</span><span style="color: #007700">(</span><span style="color: #0000BB">$token</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$lex&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Lexer</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">"[\x2c]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$token</span><span style="color: #007700">[</span><span style="color: #DD0000">"COMMA"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">"[\r][\n]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$token</span><span style="color: #007700">[</span><span style="color: #DD0000">"CRLF"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">"[\d]+"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$token</span><span style="color: #007700">[</span><span style="color: #DD0000">"DECIMAL"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">build</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"0,1,2\r\n3,42,5\r\n6,77,8\r\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">consume</span><span style="color: #007700">(</span><span style="color: #0000BB">$in</span><span style="color: #007700">);<br /><br />do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">advance</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getToken</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">Token</span><span style="color: #007700">::</span><span style="color: #0000BB">UNKNOWN&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$tok</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">LexerException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unknown&nbsp;token&nbsp;'</span><span style="color: #007700">{</span><span style="color: #0000BB">$tok</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">}</span><span style="color: #DD0000">'&nbsp;at&nbsp;offset&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">marker</span><span style="color: #007700">}</span><span style="color: #DD0000">."</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"TOKEN:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tokenIdToName</span><span style="color: #007700">[</span><span style="color: #0000BB">$tok</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}&nbsp;while&nbsp;(</span><span style="color: #0000BB">Token</span><span style="color: #007700">::</span><span style="color: #0000BB">EOI&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">$tok</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">);</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="example-4716">
   <p><strong>示例 #2 Tokenize assign statement</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">Parle</span><span style="color: #007700">\{</span><span style="color: #0000BB">Token</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Lexer</span><span style="color: #007700">};<br /><br /></span><span style="color: #0000BB">$lex&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Lexer</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">"\$[a-zA-Z_][a-zA-Z0-9_]*"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">"="</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">"\d+"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">push</span><span style="color: #007700">(</span><span style="color: #DD0000">";"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">build</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">consume</span><span style="color: #007700">(</span><span style="color: #DD0000">'$x&nbsp;=&nbsp;42;&nbsp;$y&nbsp;=&nbsp;24;'</span><span style="color: #007700">);<br /><br /><br />do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">advance</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$lex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getToken</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$tok</span><span style="color: #007700">);<br />}&nbsp;while&nbsp;(</span><span style="color: #0000BB">Token</span><span style="color: #007700">::</span><span style="color: #0000BB">EOI&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">$tok</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">);</span>
</span>
</code></div>
   </div>

  </div>
 </div></div></div></body></html>